<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;

class UsersController extends Controller{

	public function __construct(){
		$this->middleware('auth', [
			'except' => ['show', 'create', 'store', 'index', 'confirmEmail', 'followings', 'followers'],
		]);

		$this->middleware('guest', [
			'only' => ['create'],
		]);
	}

	/**
	 * 用户列表
	 *
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function index(){
		$userList = User::paginate(10);
		return view('user/index', compact('userList'));
	}

	/**
	 * 注册页面
	 *
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function create(){
		return view("user/create");
	}

	/**
	 * 显示用户信息
	 *
	 * @param User $user
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function show(User $user){
		$statuses = $user->statues()->orderBy('created_at', 'desc')->paginate(10);
		return view("user.show", compact('user', 'statuses'));
	}

	/**
	 * 用户的注册
	 *
	 * @param Request $request
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Validation\ValidationException
	 */
	public function store(Request $request){
		$this->validate($request, [
			'name'     => "required|max:50",
			'email'    => "required|email|unique:users|max:255",
			'password' => "required|confirmed|min:6",
		]);

		$user = User::create([
			'name'     => $request->name,
			'email'    => $request->email,
			'password' => bcrypt($request->password),
		]);
		$this->sendEmailConfirmationInfo($user);
		session()->flash("success", "注册成功");
		return redirect()->route("users.show", [$user]);
	}

	/**
	 *编辑资料
	 *
	 * @param User $user
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function edit(User $user){
		$this->authorize('update', $user);
		return view("user.edit", compact('user'));
	}

	/**
	 * 个人资料更新
	 *
	 * @param User    $user
	 * @param Request $request
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 * @throws \Illuminate\Validation\ValidationException
	 */
	public function update(User $user, Request $request){
		$this->authorize('update', $user);
		$this->validate($request, [
			'name'     => "required|max:50",
			'password' => "nullable|confirmed|min:6",
		]);
		$data = [];
		$data['name'] = $request->name;
		if($request->password){
			$data['password'] = bcrypt($request->password);
		}
		$user->update($data);
		session()->flash('success', '个人资料更新成功！');
		return redirect()->route('users.show', $user->id);
	}

	/**
	 * 删除用户
	 *
	 * @param User $user
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Exception
	 */
	public function destroy(User $user){
		$this->authorize('destroy', $user);
		$user->delete();
		session()->flash('success', "删出用户成功!");
		return back();
	}

	/**
	 * 验证邮箱
	 *
	 * @param $token
	 * @return \Illuminate\Http\RedirectResponse
	 */
	public function confirmEmail($token){
		$user = User::where('activation_token', $token)->firstOrFail();
		$user->activated = true;
		$user->activation_token = null;
		$user->save();
		Auth::login($user);
		session()->flash('success', '登陆成功');
		return redirect()->route('users.show', [$user]);
	}

	/**
	 * 邮箱
	 *
	 * @param $user
	 */
	protected function sendEmailConfirmationInfo($user){
		$view = 'emails.confirm';
		$data = compact('user');
		$from = 'bin774203074.com@qq.com';
		$name = 'binbin';
		$to = $user->email;
		$subject = '感谢注册';
		Mail::send($view, $data, function($message) use ($from, $name, $to, $subject){
			$message->from($from, $name)->to($to)->subject($subject);
		});
	}

	/**
	 * 我的关注列表
	 *
	 * @param User $user
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function followings(User $user){
		$users = $user->followings()->paginate(30);
		$title = $user->name."关注的人";
		return view('user.show_follow', compact('users', 'title'));
	}

	/**
	 * 我的粉丝列表
	 *
	 * @param User $user
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function followers(User $user){
		$users = $user->followers()->paginate(30);
		$title = $user->name."的粉丝";
		return view('user.show_follow', compact('users', 'title'));
	}
}
